﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace PuzzleGame.BLL
{
    public class OpreateImageBLL
    {
         public static Image[,] images { get; set; }
        public static Image[,] GetImages(Image img, int col, int row, int width, int height)
        {
            Image[,] imgs = new Image[row, col];
            int iwidth = width / col;//每块多宽
            int iheight = height / row;//每块多高
           
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col ; j++)
                {
                    #region
                    /*
                    //先初始化一个位图对象，来存储截取后的图像
                    Bitmap bmpDest = new Bitmap(iwidth, iheight, PixelFormat.Format32bppRgb);

                    //这个矩形定义了，你将要在被截取的图像上要截取的图像区域的左顶点位置和截取的大小
                    Rectangle rectSource = new Rectangle(j * iwidth, i * iheight, iwidth, iheight);

                    //这个矩形定义了，你将要把 截取的图像区域 绘制到初始化的位图的位置和大小
                    //我的定义，说明，我将把截取的区域，从位图左顶点开始绘制，绘制截取的区域原来大小
                    Rectangle rectDest = new Rectangle(0, 0, iwidth, iheight);

                    //第一个参数就是加载你要截取的图像对象，第二个和第三个参数及如上所说定义截取和绘制图像过程中的相关属性，第四个属性定义了属性值所使用的度量单位
                    // g.DrawImage(img, rectDest, rectSource, GraphicsUnit.Pixel);*/
                    #endregion
                    Bitmap bm = new Bitmap(iwidth, iheight, PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(bm);
                    g.DrawImage(img, new Rectangle(0, 0, iwidth, iheight), new Rectangle(j * iwidth, i * iheight, iwidth, iheight),GraphicsUnit.Pixel);
                    imgs[i, j] = (Image)bm;                    
                    g.Dispose();
                }
            }
            images = imgs;
            return imgs;
        }
        public static void DestoryPictureBox(Panel panel)
        {

            while (panel.Controls.Count != 0)
            {
                panel.Controls.RemoveAt(0);
            }

        }

        public static void CreateImageBoxsByRowAndColumn(Image img, int col, int row, Panel panel)
        {
            int width = img.Width / col;
            int height = img.Height / row;
            //
            Image[,] images = GetImages(img, col, row, img.Width, img.Height);
            int cou = 0;

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    PictureBox p = new PictureBox();
                    // images[i, j].Save(@"C:\"+cou+".jpg");
                    //if (panel.Controls.ContainsKey(cou.ToString()))
                    //{
                    //    p = panel.Controls.Find(cou.ToString(), true)[0] as PictureBox;
                    //}


                    p.Name = cou.ToString();
                    p.Width = width;
                    p.Height = height;
                    p.Left = j * (width + 1);
                    p.Top = i * (height + 1);
                    panel.Controls.Add(p);
                    cou++;
                    if (i == 0 && j == 0)
                    {
                        p.BackColor = Color.Gray;
                        continue;
                    }
                    p.Image = images[i, j];
                }
            }
        }

        public static bool CheckImageGamesComplate(Panel panel,int row,int col)
        {
            bool win = false;
            try
            {
                int pStart = 0;
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        PictureBox pic = panel.Controls.Find(pStart.ToString(),true)[0] as PictureBox;
                        if (i==0&&j==0)
                        {
                           
                            if (pic.Image!=null)
                            {
                                return false;
                            }
                        }
                        else  if (pic.Image==images[i,j])
                        {
                            win = true;
                        }
                        else
                        {
                            return false;
                        }
                        pStart++;
                    }
                }
            }
            catch 
            {

                return false;
            }
            return win;
        }
    }
}
